package com.cgt.mybatis.test;

import com.cgt.mybatis.pojo.Banji;
import com.cgt.mybatis.pojo.Student;
import com.cgt.mybatis.util.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyBatisDemo {
	@Test
	public void testSelectById() throws IOException {
		String resource = "mybatis.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		// 创建 SqlSessionFactory    Session:会话 (连接数据库后就建立了一次会话，有了会话就可以操作数据库)
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		// 得到SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 执行sql语句
		Student student = sqlSession.selectOne("student.selectById", 1);
		System.out.println(student);
	}

	@Test
	public void testSelectById2() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Student student = sqlSession.selectOne("student.selectById", 1);
		System.out.println(student);
	}

	@Test
	public void testSelectAll() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Student> list = sqlSession.selectList("student.selectAll");
		for (Student student : list) {
			System.out.println(student.getName());
		}
	}

	@Test
	public void testdeleteById() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		int count = sqlSession.delete("student.deleteById", 12);
		System.out.println("count:" + count);
		sqlSession.commit();
		sqlSession.close();
	}

	@Test
	public void testupdate() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Student student = new Student(12, "zhangsan", 23, "男", 1);
		int count = sqlSession.update("student.update", student);
		System.out.println("count:" + count);
		sqlSession.commit();
		sqlSession.close();
	}

	@Test
	public void testadd() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Student student = new Student(null, "zhang123", 33, "男", 2);
		int count = sqlSession.insert("student.add", student);
		System.out.println("count:" + count);
		sqlSession.commit();
		sqlSession.close();
	}

	@Test
	public void testselectbypage() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Integer pageNo = 2;
		Integer pageSize = 3;
		Integer offset = (pageNo - 1) * pageSize;
		Map<String, Object> map = new HashMap<>();
		map.put("offset", offset);
		map.put("pageSize", pageSize);
		List<Student> list = sqlSession.selectList("student.selectByPage", map);
		for (Student student : list) {
			System.out.println(student);
		}
	}

	@Test
	public void testselectTotalCount() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Integer totalCount = sqlSession.selectOne("student.selectTotalCount");
		System.out.println(totalCount);
	}

	@Test
	public void testselectStudentBanjiInfo() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Student> list = sqlSession.selectList("student.selectStudentBanjiInfo");
		for (Student student : list) {
			System.out.println(student);
//			Banji banji = student.getBanji();
		}
	}

	@Test
	public void testselectBanjiStudentInfo() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Banji> list = sqlSession.selectList("banji.selectBanjiStudentInfo");
		for (Banji banji : list) {
			System.out.println(banji);
			List<Student> studentList = banji.getList();
			for (Student student : studentList) {
				System.out.println(student);
			}
		}
	}

	//where，if标签
	@Test
	public void testselectByCondition() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Student stu = new Student();
		stu.setName("王");
		stu.setAge(23);
		stu.setGender("女");
		List<Student> list = sqlSession.selectList("student.selectByCondition", stu);
		for (Student student : list) {
			System.out.println(student);
		}
	}

	//set标签，修改提供的数据
	@Test
	public void testUpdateCondition() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Student student = new Student();
		student.setId(16);
		student.setName("小李1");
		student.setAge(24);
		int count = sqlSession.update("student.updateCondition", student);
		System.out.println(count);
		sqlSession.commit();
	}

	//foreach标签//处理数组
	@Test
	public void testDeleteAllByArray() throws IOException {
		int[] array = {13,12};
		SqlSession sqlSession = MyBatisUtil.getSqlSession();;
		int count = sqlSession.delete("student.deleteAllByArray", array);
		System.out.println("count: " + count);
		sqlSession.commit();
		sqlSession.close();
	}

	//foreach标签//处理集合
	@Test
	public void testDeleteAllByList() throws IOException {
		List<Integer> list = new ArrayList<>();
		list.add(12);
		list.add(13);
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		int count = sqlSession.delete("student.deleteAllByList", list);
		System.out.println("count: " + count);
		sqlSession.commit();
		sqlSession.close();
	}

//	choose、when、otherwise标签
	@Test
	public void testSelectByCondition1() throws IOException {
		Student student = new Student();
		student.setName("王");
		student.setAge(23);
		student.setGender("女");
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Student> list = sqlSession.selectList("student.selectByCondition1", student);
		for (Student stu : list) {
			System.out.println(stu);
		}
	}

	@Test
	public void testAddSelective() throws IOException {
		Student student = new Student();
		student.setName("王");
		student.setAge(23);
		student.setGender("女");
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		int count = sqlSession.insert("student.addSelective", student);
		System.out.println("count: " + count);
		sqlSession.commit();
		sqlSession.close();
	}

	@Test
	public void testUpdateSelective() throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Student student = new Student();
		student.setId(16);
		student.setName("小李1");
		student.setAge(25);
		student.setGender("女");
		int count = sqlSession.update("student.updateSelective", student);
		System.out.println(count);
		sqlSession.commit();
	}

	@Test
	public void testSelectByCondition3() throws IOException {
		Student student = new Student();
		student.setName("王");
		student.setAge(23);
		student.setGender("女");
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Student> list = sqlSession.selectList("student.selectByCondition3", student);
		for (Student stu : list) {
			System.out.println(stu);
		}
	}
}
